<?php
/**
 * 列表页
 */

require_once(ROOT.'/class/xml.class.php');

$xml = new Xml();
$tmp =  $xml->getXmlHeader($domain);

$dblink = new DataBase();
if(isset($_GET['cateid'])) {
    $cate_id = $_GET['cateid'];
    require_once(ROOT.'/class/booklist.class.php');
    $book_list = new BookList();
    $re = $book_list->getCateList($cate_id);
    $results = $re['results'];
    $tmp .= $re['tmp'];
}

if(isset($_GET['search'])) {
    $query = "select * from books where title like '%{$_GET['search']}%'";
    $results = $dblink->query($query);   
}

if(isset($results)) {
    if (!empty($results)) {
        foreach ($results as $result) {
            $dir_name = date('Ymd', strtotime($result['created_at']));
            if ($result['price'] == 0) {
            	$price = '价格: 免 费 ';
            } else {
                $price = "价格: {$result['price']} 书币";
            }
            
            $tmp .= "   <entry>\n";
            $tmp .= "       <title>{$result['title']}</title>\n";
            $tmp .= "       <id>tag:cnstanza.com,book#{$result['id']}</id>\n";
            $tmp .= "       <author>\n";
            $tmp .= "           <name>{$result['author']}</name>\n";
            $tmp .= "       </author>\n";
            $tmp .= "       <content type=\"xhtml\">
                                <p><span style='color: white; background-color: #46659B; background: url(); font-family: Arial, sans-serif; text-align: center; font-size: 13px; font-weight: bold; vertical-align: middle; padding-left: 9px; padding-right: 9px; padding-top: 5px; padding-bottom: 5px; border: thin soid #000000; -webkit-border-radius: 3px; text-shadow: #000000 0px 1px 0px;'>{$price}</span></p>
                                <p><span style='color: white; background-color: #46659B; background: url(); font-family: Arial, sans-serif; text-align: center; font-size: 13px; font-weight: bold; vertical-align: middle; padding-left: 9px; padding-right: 9px; padding-top: 5px; padding-bottom: 5px; border: thin soid #000000; -webkit-border-radius: 3px; text-shadow: #000000 0px 1px 0px;'>大小: {$result['size']} K</span></p>
                                <p><span style='color: white; background-color: #46659B; background: url(); font-family: Arial, sans-serif; text-align: center; font-size: 13px; font-weight: bold; vertical-align: middle; padding-left: 9px; padding-right: 9px; padding-top: 5px; padding-bottom: 5px; border: thin soid #000000; -webkit-border-radius: 3px; text-shadow: #000000 0px 1px 0px;'>下载: {$result['down']} 次</span></p>
                                <p><span style='color: white; background-color: #46659B; background: url(); font-family: Arial, sans-serif; text-align: center; font-size: 13px; font-weight: bold; vertical-align: middle; padding-left: 9px; padding-right: 9px; padding-top: 5px; padding-bottom: 5px; border: thin soid #000000; -webkit-border-radius: 3px; text-shadow: #000000 0px 1px 0px;'>制作: {$result['make']} </span></p>
				 <p><span style='color: white; background-color: #46659B; background: url(); font-family: Arial, sans-serif; text-align: center; font-size: 13px; font-weight: bold; vertical-align: middle; padding-left: 9px; padding-right: 9px; padding-top: 5px; padding-bottom: 5px; border: thin soid #000000; -webkit-border-radius: 3px; text-shadow: #000000 0px 1px 0px;'>书籍简介: </span></p>

                                {$result['content']}
                            </content>\n";
            $tmp .= "       <link type=\"application/epub+zip\" href=\"http://{$domain}/index.php?action=down&amp;bookid={$result['id']}\" />\n";
            $tmp .= "       <link rel=\"x-stanza-cover-image-thumbnail\" type=\"image/jpg\" href=\"http://{$domain}/images/small/{$dir_name}/{$result['pic']}\" />\n";
            $tmp .= "       <link rel=\"x-stanza-cover-image\" type=\"image/jpg\" href=\"http://{$domain}/images/big/{$dir_name}/{$result['pic']}\" />\n";
            $tmp .= "   </entry>\n";
            $tmp .= "   \n";
        }
    }
    
} else {
    $query = "select * from cate";
    $results = $dblink->query($query)->fetchAll();
    foreach ($results as $result) {
        $tmp .= "   <entry>\n";
        $tmp .= "       <title>{$result['title']}</title>\n";
        $tmp .= "       <id>urn:cnstanza:com</id>\n";
        $tmp .= "       <link type=\"application/atom+xml\" href=\"index.php?cateid={$result['id']}\"/>\n";
        $tmp .= "       <link rel=\"x-stanza-cover-image-thumbnail\" type=\"image/png\" href=\"theme/{$result['pic']}\" />\n";
        $tmp .= "       <content type=\"text\">收录书籍数: {$result['count']}</content>\n";
        $tmp .= "   </entry>\n";
        $tmp .= "   \n";
    }
}

$tmp .= $xml->getXmlFooter();

echo $tmp;
